<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/4
 * Time: 13:16
 */

namespace Home\Controller;


use Think\Controller;
use Think\Hook;
use Think\Page;

class UserController extends Controller
{
    public function _initialize()
    {
        $p = ['a'=> 'aa'];
        Hook::listen('action_che',$p);
    }
    public function index()
    {
        $map['user_name'] = array('like',"%".$_GET['sou']."%");

        $User = M('User');

        $count = $User->where($map)->count();
        $page = new Page($count,2);
        $show = $page->show();
        $list = $User->where($map)->limit($page->firstRow,$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();


    }

    public function addData()
    {
        $User = D('User');
        if (IS_POST){
            if ($User->create() && $User->add()){
               $this->success('添加成功','/User/index',3);
               return;
            }else{
                $arr = $User->getError();
                $this->assign('error',$arr) ;
            }
        }
        $this->display();
    }
    public function delectData($uid)
    {
        $uid = intval($uid);
        $User = D('User');
        $findData = $User->where(array(
            'uid'=>$uid,
        ))->find();
        if ($findData){
            $User->delete($uid);
            $this->success('删除成功','/User/index',3);
        }else{
            $this->error('删除失败','/User/index',3);
        }

    }
    public function saveData($uid)
    {
        $uid = intval($uid);
        $User = D('User');
        $findData = $User->where(array(
            'uid'=>$uid,
        ))->find();
        if (!$findData){
           $this->redirect('/User/index');
        }
        if (IS_POST) {
            if ($User->create() && $User->save()){
                $this->success('修改成功','/User/index',3);
                return;
            } else {
                $arr = $User->getError();
                $this->assign('error',$arr) ;
            }
        }
        $this->assign('data',$findData);
        $this->display('addData');

    }
    //测试 行为类 添加数据库
    public function addLog()
    {
        $p = [
            'uid'=>1,
            'timess'=>date("Y-m-d H:i:s",time()),
            'message'=>'干什么了'
        ];

        Hook::listen('action_log',$p);
    }



}